<template>
  <div style="width:100%;overflow:hidden">
    <weui-input :list="input_list" title="" more_label="" :tips="null" agreement_name="">
      <weui-button slot="button" @on-click="onLoginClick" type="primary">
        login
      </weui-button>
    </weui-input>
  </div>
</template>

<script>
  import WeuiInput from '../vue2componentSet/src/components/WeuiInput'
  import WeuiButton from '../vue2componentSet/src/components/WeuiButton'
  import global from '../lib/global'
  import axios from 'axios'

  export default {
    components: {
      WeuiInput,
      WeuiButton
    },
    data() {
      return {
        input_list: [{
          label: 'username'
        }, {
          label: 'password',
          type: 'password'
        }],
        redirectTo: ''
      }
    },
    methods: {
      onLoginClick() {
        if (global.is_debug) {
          this.$store.commit('login', 'heidou')
          this.redirect()
          return
        }
        if (!this.input_list[0].value) {
          this.$store.commit('toast', {
            content: '请填用户名',
            type: 'warn'
          })
          return
        }
        if (!this.input_list[1].value) {
          this.$store.commit('toast', {
            content: '请填密码',
            type: 'warn'
          })
          return
        }
        this.$store.commit('loading', {
          is_show: true,
          content: 'loading'
        })
        axios.post(global.server + 'login.do', {
          username: this.input_list[0].value,
          password: this.input_list[1].value
        }).then(result => {
          this.$store.commit('loading', {
            is_show: false
          })
          const obj = result.data
          if (obj.result == 1) {
            this.$store.commit('login', this.input_list[0].value)
            this.redirect()
          } else {
            this.$store.commit('toast', {
              type: 'warn',
              content: 'login failed'
            })
          }
        }, (response) => {
          console.log(response)
          this.$store.commit('loading', {
            is_show: false
          })
        })
      },
      redirect() {
        if (!this.redirectTo) {
          this.$router.goback()
        } else if (this.redirectTo.indexOf('#') != -1) {
          location.href = this.redirectTo
        } else {
          this.$router.redirect(this.redirectTo)
        }
      }
    },
    mounted() {
      this.$store.commit('set_title', 'login')

      var arr = location.hash.split('?')
      if (arr.length != 1) {
        arr[1].split('&').forEach(item => {
          var arr = item.split('=')
          if (arr[0] == 'redirectTo')
            this[arr[0]] = decodeURIComponent(arr[1])
        })
      }

      if (this.$store.state.username) {
        this.redirect()
      }

      if (global.is_debug)
        return
      axios.post(global.server + 'getLoginStatus.do', {}).then(result => {
        const obj = result.data
        if (obj.result == 1) {
          this.$store.commit('login', obj.username)
          this.redirect()
        } else {
          this.$store.commit('login', '')
        }
      }, (response) => {
        console.log(response)
      })
    }
  }
</script>
